import java.util.Scanner;

/**
 * @author: gao
 * @create-date: 2025/4/25 21:59
 */

public class Solution {
    public boolean lemonadeChange(int[] bills) {
        int five = 0, ten = 0;
        for (int i : bills) {
            if (i == 5) {
                five++;
            } else if (i == 10) {
                if (five == 0) return false;
                five--;
                ten++;
            } else {
                if (five != 0 && ten != 0) {
                    five--;
                    ten--;
                } else if (five >= 3) {
                    five -= 3;
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int len = in.nextInt();
            int[] bills = new int[len];
            for (int i = 0; i < len; i++) {
                bills[i] = in.nextInt();
            }
            Solution solution = new Solution();
            System.out.println(solution.lemonadeChange(bills));
        }
    }
}
